/**
 * Created by Administrator on 2017/3/29.
 */
var dao = require('../dao/MovieSearchDao');
var memberDao = require('../dao/MemberDao');
var tool = require('../../common/utils/appUtils');
var async = require('async');
var cdn = 'http://cdn.bestmovie.com.cn/MOVIE/';
var poster = '/POSTER/';
var movieDetailDao = require('../dao/MovieDetailDao');
var movieCollectionDao = require('../dao/MovieCollectionDao');
const model = require('../models/CommentModel');
module.exports = {
    // pageSize:10,
    getNewFilm: function (params, callback) {
        dao.getNewFilm(params, function (result) {
            callback(result);
        })
    },
    getHotFilm: function (parms, callback) {
        dao.getHotFilm(parms, function (reslut) {
            callback(reslut);
        })
    },
    getScoreFilm: function (params, callback) {
        dao.getScoreFilm(params, function (result) {
            callback(result);
        })
    },
    getRecomCinema: function (params, callback) {
        dao.getRecomCinema(params, function (result) {
            callback(result);
        })
    },
    /**
     * 首页的数据结构，包括 banner电影类型、地区
     * 最新、最热，推荐影吧。
     * @param params
     * @param callBack
     */
    getPageData: function (params, callBack) {

        async.parallel([
            function (callback) {
                dao.getNewFilm([0, 6], function (result) {
                    callback(null, result);
                })
            },
            function (callback) {
                dao.getScoreFilm([0, 6], function (result) {
                    callback(null, result);
                })
            },
            function (callback) {
                dao.getRecomCinema([0, 6], function (result) {
                    callback(null, result);
                })
            },
            function (callback) {
                dao.getHeaderType(12, function (result) {
                    callback(null, result);
                })
            },
            function (callback) {
                dao.getHeaderArea(12, function (result) {
                    callback(null, result);
                })
            },
            function (callback) {
                dao.getHeaderTime(12, function (result) {
                    callback(null, result);
                })
            }
        ], function (err, results) {
            if (err) {
                callback({
                    isSuccess: false
                })
                return;
            }
            // var arr=[];
            for (var i = 0; i < results[2].rows.length; i++) {
                var str = results[2].rows[i].cinema_picture;
                if (str == null || str == '') {
                    str = 'http://cdn.bestmovie.com.cn/wechat/img/1.png';
                }
                // arr.push(str);
                results[2].rows[i].cinema_picture = str;
            }
            // results[2].rows=arr;
            var datas = {
                newfilm: results[0],
                hotfilm: results[1],
                recomcinema: results[2],
                headertype: results[3],
                headerarea: results[4],
                headeryear: results[5]
            };
            callBack({
                isSuccess: true,
                data: datas
            })
        });
    },
    getFilmbyType: function (params, callback) {
        if (!params.length > 0) {
            callback();
            return;
        }
        var pageSize = 10;
        var index = params[0];
        var page = params[1];

        if (index == 1) {
            dao.getNewFilm([pageSize * page, pageSize], function (result) {
                callback(result);
            });
        }

        // dao.getFilmByType(params,function (result) {
        //     callback(result);
        // })
    },
    getMovieSearchlib: function (params, callback) {

        var index = parseInt(params);
        tool.baseDao('getMovieSearchlib', 'select * from movie limit ?,?', [index, 10], function (result) {
            if (result.isSuccess) {
                var movieInfo = [];
                result.rows.forEach(function (val) {
                    movieInfo.push({
                        mid: val.mid,                       //影片编号
                        movie_name: val.movie_name,         //影片名字
                        director: val.director,              //导演
                        brief: val.brief,                    //详情介绍
                        cast: val.cast,                    //演员
                        score: val.score,                    //评分
                        country: val.country,                //地区
                        category: val.category,               //类型-标签
                        publishdate: val.publishdate,        //影片-年
                        poster: cdn + val.mid + poster + val.poster
                    });

                });

                result.rows = movieInfo;
                callback(result)
            }
        })

    },


    showMovieLibByParams: function (params, callback) {

        var sql = 'SELECT movie.poster, movie.mid,  movie.movie_name, movie.director,  movie.brief,  ' +
            'movie.cast,    movie.score,   movie.country,  movie.category,   date_format(movie.publishdate, "%Y") AS publishdate,  movie.movie_bigpic  FROM   movie   WHERE  ';
        var id = params[0];
        var pname = params[1];

        if (id > 100 && id < 200) {
            sql += " category like '%" + pname + "%' and "
        } else if (id > 200) {
            sql += "  country like '%" + pname + "%' and "
        }
        sql += "movie.isdeleted != 1   GROUP BY  movie.movie_name   ORDER BY   movie.publishdate DESC     LIMIT ?, 10";

        if (id == 2) {
            sql = sql.replace('movie.publishdate DESC', 'movie.score DESC');
            var i = 2;
        }

        tool.baseDao('showMovieLibByParams', sql, 0, function (result) {
            callback(result);
        })
    },
    getMovieMultiLib: function (params, callback) {
        dao.getMovieLib(params, function (result) {
            callback(result);
        })
    },

    /**
     * 影吧片库 --> 影片详情
     * @param req
     * @param callback
     * http -- get :framework7 a标签
     */
    getMovieDetailInfoModel: function (req, params, callback) {
        var mid = '';
        if (req.query.mid) {
            mid = req.query.mid;
        }
        // if (params)
        var openid=params[0];
        var lat=params[1];
        var longt=params[2];
        // if(req.signedCookies.openid){
        //     open_id = req.signedCookies.openid
        // }    var open_id ='0';

            async.parallel({
                movie: function (callBack) {
                    movieDetailDao.getMovieInfo(mid, function (data) {
                        if (data.isSuccess) {
                            if (data.rows.length > 0) {
                                var movieInfo = [];
                                data.rows.forEach(function (val) {
                                    var filmTime = (Math.floor(val.filmlength / 60) + '小时') + (val.filmlength % 60 + '分');
                                    var movie_bigpic;
                                    if (val.movie_bigpic) {
                                        movie_bigpic = cdn + val.mid + poster + val.movie_bigpic;
                                    }
                                    movieInfo.push({
                                        mid: val.mid,
                                        movie_name: val.movie_name,
                                        filmlength: filmTime,
                                        score: val.score,
                                        category: val.category,
                                        publishdate: val.publishdate.format('yyyy-mm-dd'),
                                        director: val.director,
                                        country: val.country,
                                        cast: val.cast,
                                        brief: val.brief,
                                        poster: cdn + val.mid + poster + val.poster,
                                        movie_bigpic: movie_bigpic
                                    });
                                    callBack(null, movieInfo[0]);
                                });
                            }
                        } else {

                        }
                    });
                },
                isCollection: function (callBack) {
                    memberDao.selectByopenid(openid, function (data) {
                        if (data.rows[0]) {
                            var member_id = data.rows[0].member_id;
                            movieCollectionDao.selectMovieCollectionByMemberidAndMid([member_id, mid], function (data) {
                                if (data.isSuccess) {
                                    if (data.rows[0]) {
                                        callBack(null, true);
                                    } else {
                                        callBack(null, false);
                                    }
                                } else {
                                    callBack(null, false);
                                }
                            })
                        } else {
                            callBack(null, false);
                        }
                    })
                },
                cinema: function (callBack) {
                    dao.getCinemaByMid([mid,lat,longt], function (result) {
                        if (result.isSuccess) {
                            callBack(null, result);
                        }
                    })
                },
                comments:function (callBack) {
                    model.postCommentListNew([mid,1],function (result) {
                        if(result.data&&result.data.hasOwnProperty('rows')){
                            callBack(null,result.data.rows)
                        }else{
                            callBack(null,null)
                        }

                    })
                }
            }, function (err, result) {
                if (err) {
                    resultData = {
                        layout: false,
                        message: 'error',
                        status: 'error'
                    };
                } else {
                    for (var i = 0; i < result['cinema'].rows.length; i++) {
                        var str = result['cinema'].rows[i].cinema_picture;
                        if (str == null || str == '') {
                            str = 'http://cdn.bestmovie.com.cn/wechat/img/1.png';
                        }
                        // arr.push(str);
                        result['cinema'].rows[i].cinema_picture = str;
                    }
                    resultData = {
                        layout: false,
                        message: 'success',
                        status: 'success',
                        movie: result.movie,
                        cinemas: result.cinema.rows,
                        isCollection: result.isCollection,
                        open_id: openid,
                        comments:result.comments
                    };
                }
                callback(resultData);
            });

    }
};